package com.mutual.base.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang3.StringUtils;

/**
 * 日期工具类
 * @create-time 2016年3月4日 上午10:48:38
 * @version     1.0
 */
public class DateUtil {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	private static String[] weeks = new String[]{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
	private static Calendar calendar = Calendar.getInstance(Locale.CHINA);
	/** 格式化年月字符串 */
	public static String fmtYM = "yyyyMM";
	public static String fmtY_M_d = "yyyy-MM-dd";

	public static String fmtYMdHms = "yyyy年M月d日 HH点mm分ss秒";
	public static String fmtY_M_d_H_m_s = "yyyy-MM-dd HH:mm:ss";
	
	public static String formatDate(Date d,String pattern){
		if(d == null){
			return "";
		}
		sdf.applyPattern(pattern);
		return sdf.format(d);
	}
	
	public static Integer fmtYMDate(Date d){
		if(d == null){
			return 0;
		}
		sdf.applyPattern(fmtYM);
		
		Integer num = Integer.parseInt(sdf.format(d));
		return num;
	}
	

	public static Date parseYMDate(Integer date) {
		sdf.applyPattern(fmtYM);
		
		Date d = null;
		if(null == date){
			return d;
		}
		try {
			d = sdf.parse(date.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}   
	
	public static Date parseDate(String date, String pattern) {
		if(StringUtils.isBlank(date)){
			return null;
		}
		sdf.applyPattern(pattern);
		Date d = null;
		try {
			d = sdf.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return d;
	}
	
	/**
	 * 获取整点日期
	 * @param date
	 * @return
	 */
	public static Date getDayZeroTime(Date date){
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND,0);
		
		return calendar.getTime();
	}
	
	public static Date parseDate(String date, String pattern,Locale locale) {
		Date result = null;
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern,locale);
			result = simpleDateFormat.parse(date);
			return result;
		} catch (ParseException e) {
			try {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd",locale);
				result = simpleDateFormat.parse(date);
				return result;
			} catch (ParseException e1) {
				e1.printStackTrace();
				return result;
			}
		}
		
	}
	
	/**
	 * 获取是周几 1表示周1 0表示周日
	 * @param d
	 * @return
	 */
	public static int getChineseWeekNum(Date d){
		calendar.setTime(d);
		
		int week = calendar.get(Calendar.DAY_OF_WEEK);
		
		week = week - 1;	//默认周日为1
		
		return week;
	}
	
	/**
	 * 获取星期几
	 * @param num
	 * @return
	 */
	public static String getChineseWeekNameByNum(int num){
		return weeks[num];
	}

	/**
	 * 获取年份
	 * @param now
	 * @return
	 */
	public static int getYear(Date now) {
		Calendar c = Calendar.getInstance(Locale.CHINA);
		c.setTime(now);
		int year = c.get(Calendar.YEAR);
		
		return year;
	}

	/**
	 * 判断是否超过多少分钟
	 * 
	 * @param start
	 * @param end
	 * @param subMinute 相差的分钟
	 * @return boolean
	 */
	public static boolean judgmentDate(Date start, Date end, int subMinute) {
		long cha = end.getTime() - start.getTime();
		if (cha < 0) {
			return false;
		}
		double result = cha * 1.0 / (1000 * 60);
		if (result <= subMinute) {
			return true;
		}
		return false;
	}
	/**
	 * 获取
	 * @param now
	 * @return
	 */
	public static int getPrizeWeekByTime(Date now) {
		Date compareDate = getDayZeroTime(now);
		int week = 1;	//默认第一周
		Calendar c = Calendar.getInstance(Locale.CHINA);
		c.setTime(new Date());
		c.set(Calendar.MONTH,0);
		c.set(Calendar.DAY_OF_MONTH, 12);	//一月12日
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND,0);
		
	//	Date firstWeekBegin = c.getTime();
		c.set(Calendar.DAY_OF_MONTH, 18);	//一月18日
		Date firstWeekEnd = c.getTime();
	//	c.set(Calendar.DAY_OF_MONTH, 19);	//一月19日
	//	Date secondWeekBegin = c.getTime();
		c.set(Calendar.DAY_OF_MONTH, 25);	//一月25日
		Date secondWeekEnd = c.getTime();
	//	c.set(Calendar.DAY_OF_MONTH, 26);	//一月26日
	//	Date thirdWeekBegin = c.getTime();
		c.set(Calendar.MONDAY, 1);	//二月
		c.set(Calendar.DAY_OF_MONTH, 1);	//二月1日
		Date thirdWeekEnd = c.getTime();
	//	c.set(Calendar.DAY_OF_MONTH, 2);	//二月2日
	//	Date fourthWeekBegin = c.getTime();
		c.set(Calendar.DAY_OF_MONTH, 8);	//二月8日
		Date fourthWeekEnd = c.getTime();
	//	c.set(Calendar.DAY_OF_MONTH, 9);	//二月9日
	//	Date fifthWeekBegin = c.getTime();	
	//	c.set(Calendar.DAY_OF_MONTH, 15);	//二月15日
	//	Date fifthWeekEnd = c.getTime();
		
		
		if(!compareDate.after(firstWeekEnd)){
			week = 1;
		}else if(!compareDate.after(secondWeekEnd)){
			week = 2;
		}else if(!compareDate.after(thirdWeekEnd)){
			week = 3;
		}else if(!compareDate.after(fourthWeekEnd)){
			week = 4;
		}else{
			week = 5;
		}
		
		
		return week;
		
	}
	
	/**
	 * 计算两个日期之间的天数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int daysBetween(Date startDate, Date endDate){
		int day = 0;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar startCalendar = Calendar.getInstance();
			Calendar endCalendar = Calendar.getInstance();
			startCalendar.setTime(sdf.parse(formatDate(startDate, "yyyy-MM-dd")));
			endCalendar.setTime(sdf.parse(formatDate(endDate, "yyyy-MM-dd")));
			Long diff = (endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / (1000*60*60*24);
			day = diff.intValue();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return day;
	}
	
	/**
	 * 获取区间日期
	 * @param startDate		日期
	 * @param beweenDay		区间天数，可以为负数
	 * @return
	 */
	public static List<String> getBetweenDate(Date date,int betweenDay){
		List<String> result = new ArrayList<String>();
		
		// 指定日期封装为Calendar
		Calendar beginCalendar = Calendar.getInstance(Locale.CHINA);
		beginCalendar.setTime(date);
		
		// 开始/结束 日期
		Calendar endCalendar = Calendar.getInstance(Locale.CHINA);
		endCalendar.setTime(date);
		endCalendar.add(Calendar.DAY_OF_YEAR, betweenDay);
		
		if(beginCalendar.after(endCalendar)){
			beginCalendar = endCalendar;
		}
		
		if(betweenDay < 0) betweenDay *= -1;
		
		String dateStr = null;
		for (int i = 0; i < betweenDay; i++) {
			beginCalendar.add(Calendar.DAY_OF_YEAR, 1);
			
			dateStr = formatDate(beginCalendar.getTime(), fmtY_M_d);
			result.add(dateStr);
		}
		return result;
	}
}
